#include "CGamePlayState.h"
#include "CMainMenuState.h"
#include "CGame.h"
#include "CUnit.h"
#include "CHero.h"
#include "CResource.h"
#include "CGameOverState.h"
#include "CWinState.h"
#include "SGD Wrappers/CSGD_Direct3D.h"
#include "SGD Wrappers/CSGD_TextureManager.h"
#include "SGD Wrappers/CSGD_DirectSound.h"
#include "SGD Wrappers/CSGD_WaveManager.h"
#include "SGD Wrappers/CSGD_DirectInput.h"
#include "CObjectManager.h"
#include "Mail System.h"
#include "Deliverables.h"
#include "CLevel.h"
#include "CObjGenerator.h"
#include <string>
using std::string;


CGamePlayState::CGamePlayState()
{
	m_pCurrLevel = NULL;
}
CGamePlayState::~CGamePlayState()
{

}
CGamePlayState* CGamePlayState::GetInstance()
{
	static CGamePlayState instance;
	return &instance;
}
void CGamePlayState::Enter()
{
	ObjectGenerator<string, CBase>::GetInstance()->RegisterClassType<CBase>("CBase");
	ObjectGenerator<string, CBase>::GetInstance()->RegisterClassType<CHero>("CHero");
	ObjectGenerator<string, CBase>::GetInstance()->RegisterClassType<CUnit>("CUnit");
	ObjectGenerator<string, CBase>::GetInstance()->RegisterClassType<CResource>("CResource");

	m_pCurrLevel = new CLevel;
	m_pCurrLevel->Enter("Levels/Castle.rmf");
	MailSystem::GetInstance()->SendMail(new SpawnHero());
	MailSystem::GetInstance()->SendMail(new SpawnResources());
}
bool CGamePlayState::Input()
{
	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_ESCAPE))
	{
		CGame::GetInstance()->PopState();
	}
	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_2))
	{
		CGame::GetInstance()->PushState(CGameOverState::GetInstance());
	}
	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_3))
	{
		CGame::GetInstance()->PushState(CWinState::GetInstance());
	}
	return true;
}
void CGamePlayState::Exit()
{
	ObjectGenerator<string, CBase>::GetInstance()->UnregisterClassType("CBase");
	ObjectGenerator<string, CBase>::GetInstance()->UnregisterClassType("CHero");
	ObjectGenerator<string, CBase>::GetInstance()->UnregisterClassType("CUnit");
	ObjectGenerator<string, CBase>::GetInstance()->UnregisterClassType("CResource");
	ObjectGenerator<string, CBase>::GetInstance()->ShutdownObjectFactory();
	m_pCurrLevel->Exit();
	delete m_pCurrLevel;
	CObjectManager::GetInstance()->Clear();
}
void CGamePlayState::Update(float fDelta)
{
	CObjectManager::GetInstance()->CheckDistances(fDelta);
	CObjectManager::GetInstance()->Update(fDelta);
	CObjectManager::GetInstance()->CheckCollisons();
	
}
void CGamePlayState::Render()
{
	if(m_pCurrLevel != NULL)
		m_pCurrLevel->Render();
	CObjectManager::GetInstance()->Render();
}